package com.xaicode.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.xaicode.auth.annotation.Log;
import com.xaicode.auth.domain.Result;
import com.xaicode.auth.domain.params.IdsParam;
import com.xaicode.auth.entity.SysPost;
import com.xaicode.auth.enums.BusinessType;
import com.xaicode.auth.security.domain.params.SysPostQueryParam;
import com.xaicode.auth.service.ISysPostService;
import com.xaicode.auth.utils.GlobalValidator;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 岗位管理接口
 * 
 * @date 2024/12/19
 */
@RestController
@RequestMapping("/api/system/post")
public class SysPostController {

    @Autowired
    private ISysPostService sysPostService;


    @Log(title = "查看岗位列表", businessType = BusinessType.QUERY, isSaveResponseData = false)
    @SaCheckPermission(value = {"system:post:list", "system:user:list"}, mode = SaMode.OR)
    @PostMapping("/page")
    public Result<?> page(@RequestBody SysPostQueryParam param) {
        PageInfo<SysPost> pageInfo = sysPostService.queryPage(param);
        return Result.ok(pageInfo);
    }

    @Log(title = "新增岗位", businessType = BusinessType.INSERT)
    @SaCheckPermission("system:post:add")
    @PostMapping("/create")
    public Result<?> create(@RequestBody SysPost entity) {
        sysPostService.save(entity);
        return Result.ok();
    }

    @Log(title = "修改岗位", businessType = BusinessType.UPDATE)
    @SaCheckPermission("system:post:edit")
    @PostMapping("/update")
    public Result<?> update(@Validated @RequestBody SysPost entity) {
        GlobalValidator.notNull(entity.getId(), "id不能为空");
        sysPostService.updateById(entity);
        return Result.ok();
    }

    @Log(title = "删除岗位", businessType = BusinessType.DELETE)
    @SaCheckPermission("system:post:del")
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody IdsParam<Integer> ids) {
        List<Integer> idList = ids.getIds();
        sysPostService.removeByIds(idList);
        return Result.ok();
    }

}
